import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueDevTools from 'vite-plugin-vue-devtools'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'
import ElementPlus from 'unplugin-element-plus/vite'
import { AntDesignXVueResolver } from 'ant-design-x-vue/resolver';
// import postCssPxToRem from 'postcss-pxtorem'

// https://vite.dev/config/
export default defineConfig({

    plugins: [
        vue(),
        vueDevTools(),
        // ElementPlus({
        //     useSource: true
        // }),
        AutoImport({
            resolvers: [ElementPlusResolver({ importStyle: 'sass' })],   // ELMessageBox, ELMessage...-
            imports: ['vue', 'vue-router', 'pinia'], // ref, computed...
            dts: 'src/auto-imports.d.ts',
        }),
        Components({
            resolvers: [ElementPlusResolver({
                importStyle: 'sass'
            }), AntDesignXVueResolver()], // element-plus 组件
            dts: 'src/components.d.ts',
            dirs: ['src/components', 'src/icons'],
        }),

    ],

    css: {
        preprocessorOptions: {
            scss: {
                additionalData: `@use "@/styles/index.scss" as *;`,
                // api: 'modern-compiler',
            },
        },
    },
    resolve: {
        alias: {
            '@': fileURLToPath(new URL('./src', import.meta.url))
        },
    },

    server: {
        // port: 3000,
        host: '0.0.0.0',
        open: true,
        proxy: {
            '/api': {
                target: 'http://localhost:8080',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/api/, '')
            },
            // 地图文件代理
            '/tile': {
                target: 'http://10.30.222.55:7788/tile',
                // target: 'http://127.0.0.1:8079/tile',
                changeOrigin: true,
                rewrite: (path) => path.replace(/^\/tile/, ''),
            },
        }
    },

    // css: {
    //     // postcss: {
    //     //   plugins: [
    //     //     postCssPxToRem({
    //     //       rootValue: 16,
    //     //       propList: ['*'],
    //     //     })
    //     //   ]
    //     // }
    //     preprocessorOptions: {
    //         scss: {
    //             // 自动导入定制化样式文件进行样式覆盖
    //             additionalData: `
    //       @use "@/styles/element/index.scss" as *;
    //       @use "@/styles/var.scss" as *;
    //     `,
    //         },
    //     }
    // },
})
